﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Pivot;
using Microsoft.Pivot.Internal.Views;
using System.Collections.Generic;
namespace SLPivotViewer
{
    public delegate List<CustomAction> CustomActionDelegate(string itemId);

    public class PivotViewerSP : PivotViewer
    {
        pivotLoading _pvLoading;
        public PivotViewerSP()
        {
            registeredCustActDelegates = new List<CustomActionDelegate>();
 
        }
        private List<CustomActionDelegate> registeredCustActDelegates;

        #region Custom Actions
        public void RegisterCustomActionDelegate(CustomActionDelegate custActDelegate)
        {
            if (!registeredCustActDelegates.Contains(custActDelegate))
                registeredCustActDelegates.Add(custActDelegate);
        }

        public void UnregisterCustomActionDelegate(CustomActionDelegate custActDelegate)
        {
            if (registeredCustActDelegates.Contains(custActDelegate))
                registeredCustActDelegates.Remove(custActDelegate);
        }

        protected override List<CustomAction> GetCustomActionsForItem(string itemId)
        {
            List<CustomAction> actions = new List<CustomAction>();

            foreach (var del in registeredCustActDelegates)
            {
                actions.AddRange(del(itemId));
            }

            return actions;
        }
        #endregion

        private Grid GridContainer { get; set; }
        private Grid GridViews { get; set; }
        Brush _bruFilterBackground;
        public Brush FilterBackground
        {
            get {

                return _bruFilterBackground;

            }
            set {
                _bruFilterBackground = value;
            }
        }

        Stretch _filterStretch;
        public Stretch FilterStretch
        {
            get {
                return _filterStretch;
            }
            set {
                _filterStretch = value;
            }
        }
        Brush _bruTitleBarBackground;
        public Brush TitleBarBackground
        {
            get {
                return _bruTitleBarBackground;
            }
            set {
                _bruTitleBarBackground = value;
             }
        }

        private Stretch _titleBarStretch;
        public Stretch TitleBarStretch
        {
            get
            {
                return _titleBarStretch;
            }
            set
            {
                _titleBarStretch = value;
            }
        }
        Brush _bruInfoBackground;

        public Brush InfoBackground
        {
            get
            {
                return _bruInfoBackground;
            }
            set
            {
                _bruInfoBackground = value;
           }
        
        }

        private Stretch _infoStretch;
        public Stretch InfoStretch
        {
            get
            {
                return _infoStretch;
            }
            set
            {
                ((Rectangle)((Grid)((Grid)((InfoPaneView)this.GridViews.Children[3]).Content).Children[1]).Children[0]).Stretch = value;
                _infoStretch = value;
            }
        }

        private Visibility _filterVisibility;
        public Visibility FilterVisibility
        {
            get
            {
                return _filterVisibility;
            }
            set
            {
                _filterVisibility = value;
            }
        }


        private Visibility _infoVisibility;
        public Visibility InfoVisibility
        {
            get
            {
                return _infoVisibility;
            }
            set
            {
                _infoVisibility = value;
            }
        }

        private Visibility _titleBarVisibility; 

        public Visibility TitleBarVisibility
        {
            get
            {
                return _titleBarVisibility;
            }
            set
            {
                _titleBarVisibility = value;
            }
        }
        Brush _bruContentBackground;
        public Brush ContentBackground
        {
            get { return _bruContentBackground; }
            set {
                _bruContentBackground = value;
            }
        }


        private Stretch _contentStretch;
        public Stretch ContentStretch
        {
            get { return _contentStretch; }
            set
            {
                _contentStretch = value;
            }
        }

        #region Custom Loading Visual





        #endregion

        public override void OnApplyTemplate()
        {

            base.OnApplyTemplate();
            _pvLoading = new pivotLoading();
            // Locate UI Elements
            Grid partContainer = (Grid)this.GetTemplateChild("PART_Container");
            CollectionViewerView cvv = ((CollectionViewerView)(partContainer).Children[0]);
            Grid container = cvv.Content as Grid;
            Grid viewerGrid = container.Children[1] as Grid;
            Grid LoadingVisualContainer = ((Grid)((Grid)cvv.FindName("LayoutRoot")).Children[3]);

            Grid cont = LoadingVisualContainer;
            _pvLoading.Name = "PART_LoadingVisual";

            cont.VerticalAlignment = VerticalAlignment.Stretch;
            cont.HorizontalAlignment = HorizontalAlignment.Stretch;
            cont.Children.RemoveAt(0);
            cont.Children.Add(_pvLoading);



            this.GridContainer = container;
            this.GridViews = viewerGrid;
            if (_bruContentBackground != null)
                GridContainer.Background = _bruContentBackground;
            if (_bruInfoBackground != null)
                ((Rectangle)((Grid)((Grid)((InfoPaneView)this.GridViews.Children[3]).Content).Children[1]).Children[0]).Fill = _bruInfoBackground;
            if (_bruTitleBarBackground != null)
                ((Rectangle)((Grid)((ControlBarView)(GridContainer.Children[2])).Content).Children[0]).Fill = _bruTitleBarBackground;
            if (_bruFilterBackground != null)
            {
                ((Rectangle)((Grid)((FilterPaneView)((Grid)GridViews.Children[2]).Children[0]).Content).Children[1]).Fill = _bruFilterBackground;
                ((Rectangle)((Grid)((FilterPaneView)((Grid)GridViews.Children[2]).Children[0]).Content).Children[0]).Fill = _bruFilterBackground;
            }

            GridViews.Children[2].Visibility = _filterVisibility;
            if (_filterVisibility == Visibility.Collapsed)
            {
                GridViews.ColumnDefinitions[0].Width = new GridLength(0);
            }
            else
            {
                GridViews.ColumnDefinitions[0].Width = new GridLength(190);
            }

            if (_infoVisibility == Visibility.Collapsed)
            {
                GridViews.ColumnDefinitions[2].Width = new GridLength(0);
            }
            else
            {
                GridViews.ColumnDefinitions[2].Width = GridLength.Auto;
            }

            ((ControlBarView)(GridContainer.Children[2])).Visibility = _titleBarVisibility;
            if (_titleBarVisibility == Visibility.Collapsed)
            {
                GridContainer.RowDefinitions[0].Height= new GridLength(0);
            }
            else
            {
                GridContainer.RowDefinitions[0].Height = new GridLength(30);
            }

            
        }
        

    }
}
